﻿using System.Linq;
using EasyIot.Model;

namespace EasyIot.Service
{
    public class DeviceService : BaseService<DeviceModel>
    {
        public static dynamic GetListByFilter(DeviceModel filter, PageInfo pageInfo)
        {
            pageInfo.prefix = "";
            string _where = "device";
            if (!string.IsNullOrEmpty(filter.DeviceCode))
            {
                _where += string.Format(" and {0}DeviceCode like concat('%',@DeviceCode,'%')", pageInfo.prefix);
            }
            if (!string.IsNullOrEmpty(filter.DeviceName))
            {
                _where += string.Format(" and {0}DeviceName like concat('%',@DeviceName,'%')", pageInfo.prefix);
            }
            pageInfo.returnFields = string.Format("{0}Id,{0}SortCode,{0}CreateTime,{0}DeviceCode,{0}DeviceName,{0}LastActive", pageInfo.prefix);
            return GetPageUnite(filter, pageInfo, _where);
        }

        public static bool IsExistCode(string DeviceCode)
        {
            string where = "where DeviceCode=@DeviceCode";
            return GetByWhere(where, new { DeviceCode = DeviceCode }).Count() > 0 ? true : false;
        } 
    }
}
